package com.xiaoyu.serviceImpl;

import com.xiaoyu.AccountDao;
import com.xiaoyu.PostDao;
import com.xiaoyu.TeacherDao;
import com.xiaoyu.client.Account_PostDao;
import com.xiaoyu.client.Class_TeacherDao;
import com.xiaoyu.client.Class_Teacher_CourseDao;
import com.xiaoyu.model.Account;
import com.xiaoyu.model.Post;
import com.xiaoyu.model.Teacher;
import com.xiaoyu.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.List;

@Service
@Transactional
public class TeacherServiceImpl implements TeacherService {
    @Autowired
    private TeacherDao teacherDao;
    @Autowired
    private Class_Teacher_CourseDao classTeacherCourseDao;
    @Autowired
    private Class_TeacherDao classTeacherDao;
    @Autowired
    private AccountDao accountDao;
    @Autowired
    private PostDao postDao;
    @Autowired
    private Account_PostDao accountPostDao;

    @Override
    public List<Teacher> allTeacher() {
        return teacherDao.findTeacher(null);
    }

    @Override
    public Boolean deleteTeacher(Teacher teacher) {
        if(teacherDao.deleteTeacher(teacher)==1)
            return true;
        return false;
    }

    @Override
    public Boolean updateTeacher(Teacher teacher) {
        if (teacherDao.updateTeacher(teacher)==1)
            return true;
        return false;
    }

    @Override
    public Boolean addTeacher(Teacher teacher,String post,Account account) {

        //获取aid
        Account a=accountDao.findOneAccount(account);
        teacher.setAid(a.getId());

        //设置账户权限
        Post p=new Post();
        p.setPname(post);
        p=postDao.findOnePost(p);
        accountPostDao.add(teacher.getAid(),p.getId());

        if (teacherDao.addTeacher(teacher)==1)
            return true;
        return false;
    }

    @Override
    public Teacher findOneTeacher(Teacher teacher) {
        return teacherDao.findOneTeacher(teacher);
    }

    @Override
    public Teacher findTeacher(Teacher teacher) {
        return teacherDao.findTeacher(teacher).get(0);
    }
    @Override
    public Boolean addTeacherClient(Teacher teacher,Integer courseId, Integer cid) {
        Teacher t=teacherDao.findTeacher(teacher).get(0);
        classTeacherDao.add(cid,t.getId());
        classTeacherCourseDao.add(cid,t.getId(),courseId);
        return true;
    }

    @Override
    public Boolean deleteTeacherClient(Teacher teacher) {
        classTeacherDao.delete(teacher.getId());
        classTeacherCourseDao.delete(teacher.getId());
        return true;
    }

}
